package com.example.administrator.xtu_community.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.administrator.xtu_community.R;
import com.example.administrator.xtu_community.activity.AddNoteActivity;
import com.example.administrator.xtu_community.activity.SettingActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;


/**
 * @author an
 */
public class MyFragment extends Fragment implements View.OnClickListener {

    private Context context;
    private Toolbar toolbar;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this.getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_my, container, false);
        Button setting = (Button) view.findViewById(R.id.my_set_button);
        setting.setOnClickListener(this);
        Button note = (Button) view.findViewById(R.id.my_note_button);
        note.setOnClickListener(this);
        Button collect = (Button) view.findViewById(R.id.my_collect_button);
        collect.setOnClickListener(this);
        toolbar = view.findViewById(R.id.my_toolbar);
        toolbar.inflateMenu(R.menu.my_toolbar);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.my_toolbar_share:
                        Toast.makeText(getContext(), "Share", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                }
                return false;
            }
        });

        FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.my_fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getContext(), "remind", Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.my_set_button:
                Intent intent = new Intent(getContext(), SettingActivity.class);
                startActivity(intent);
                break;
            case R.id.my_note_button:
                replaceFragment(new MyFragmentNote());
                break;
            case R.id.my_collect_button:
                replaceFragment(new MyFragmentCollect());
                break;
            default:
                break;
        }
    }

    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.commit();
    }

}